package
{
	import core.Model;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import model.card.CardData;
	import model.card.ICardData;
	
	import utils.ai.CardTools;
	
	import view.card.Card;
	import view.card.ICard;
	import view.components.Button;
	import view.components.ImgButton;
	
	public class TestGDY extends Sprite
	{
		private var cards:Array;
		private var currentCards:Array;
		private var toY:int;
		private var cardsData:Array;
		
		public function TestGDY()
		{
			initCars();	
			initButtons();
		}
		
		private function initButtons():void
		{
			var getBut:Button = new ImgButton(500,200,0,onGetBut,200,20,"card_back","card_spades");
			addChild(getBut);
		}
		
		private function onGetBut(event:MouseEvent):void
		{
			currentCards = getRondom5Cards();
			
			CardTools.sortCards(currentCards);
			show5Cards();
		}
		
		public function getRondom5Cards():Array
		{
			var arr:Array = [];
			for (var i:int=0; i<13; i++)
			{
				var index:int = cards.length * Math.random();
				var card:ICard = cards.splice(index,1)[0] as ICard;
				arr.push(card);
			}
			return arr;
		}
		
		public function getRondom5CardsData():Array
		{
			var arr:Array = [];
			var cardData:ICardData;
			for (var i:int=0; i<13; i++)
			{
				if (cardsData.length > 3)
				{
					cardData = cardsData.splice(3,1)[0] as ICardData;
				}
				else
				{
					var index:int = cardsData.length * Math.random();
					cardData = cardsData.splice(index,1)[0] as ICardData;
				}
				arr.push(cardData);
			}
			return arr;
		}
		
		private function show5Cards():void
		{
			
			toY += 100;
			for (var i:int=0; i<currentCards.length; i++)
			{
				currentCards[i].x = i * 20;
				currentCards[i].y = toY;
				addChild(currentCards[i].self); 
			}
		}
		
		public function getCards():Array
		{
			return cards.concat();
		}
		
		public function show():void
		{
			for (var i:int=0; i<4; i++)
			{
				Model.getInstance().seatModel.updateSeatHoldCards(getRondom5CardsData(),i);
			}
		}
		
		private function initCars():void
		{
			cards = [];
			cardsData = [];
			var j:int = 2;
			var card:ICard;
			var cardData:ICardData
			for (var i:int=0; i< 52; i++)
			{
				if (i % 4 == 0)
				{
					j++;
				}
				
				card = new Card();
				cardData = new CardData();
				cardData.id = i;
				
				cardsData.push(cardData);
				if ( i > 51)
				{
					cardData.type = i - 48;	
				}
				else
				{
					cardData.type = i % 4;
				}
				cardData.value = j;
				card.updateData(cardData);
				
				
				card.self.x =  20 * i;
				
				addChild(card.self);
				cards.push(card);
			}
			j++;
			for (;i<54;i++)
			{
				
				card = new Card();
				cardData = new CardData();
				cardData.id = i;
				cardData.type = i - 48;
				cardData.value = j;
				card.updateData(cardData);
				
				cardsData.push(cardData);
				card.self.x =  20 * i;
				
				addChild(card.self);
				cards.push(card);
				
			}
		}
	}
}